//
//  PTStudySAppointmentViewController.m
//  PT
//
//  Created by yate1996 on 15/6/19.
//  Copyright (c) 2015年 jarze. All rights reserved.
//

#import "PTStudySAppointmentViewController.h"
#import "MeItem.h"
#import "PTTeacherMessageView.h"
#import "PTStudyAppointmentTimeSelectCell.h"
#import "PTRecorderViewController.h"
#import "TeacherViewController.h"

@interface PTStudySAppointmentViewController ()<UITableViewDataSource,UITableViewDelegate,PTRecorderButtonDelegate>
@property(nonatomic, strong) UIView *mottoView;
@property(nonatomic, strong) UIView *weekTiemView;
@property(nonatomic, strong) UITableView *timeSelectView;
@property(nonatomic, strong) PTRecorderViewController *recoderVc;
@property(nonatomic, strong) UIButton *lastSelectedButton;
@property (nonatomic, strong) NSIndexPath *lastTimeSelectindexPath;

@end

@implementation PTStudySAppointmentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = PTColor(245, 245, 245, 1.0);
    
    [self setupTeacherMessageView];
    
    [self setupWeekTime];
    
    [self setupTimeSelectView];
    
    [self 约课];

}

-(void)约课
{
    UIView *testLessonView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.timeSelectView.frame), 320, 56)];
    testLessonView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"yuekeBG"]];
    
    UILabel *lessonNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 12, 250, 20)];
    lessonNameLabel.textColor = [UIColor redColor];
    lessonNameLabel.font = [UIFont systemFontOfSize:13];
    NSString *string = @"Test Lesson";
    lessonNameLabel.text = string;
    [lessonNameLabel sizeToFit];
    
    UILabel *lessonTimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 35, 250, 20)];
    lessonTimeLabel.font = PTRecorderlessonTitleFont;
    NSString *timeLessonString = @"May,17th,2015 10:00-10:25";
    lessonTimeLabel.text = timeLessonString;
    [lessonTimeLabel sizeToFit];
    
    UIImage *image = [UIImage imageNamed:@"yueke"];
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(testLessonView.width - image.size.width - 15, 15, image.size.width, image.size.height)];
    [button addTarget:self action:@selector(doneButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [button setBackgroundImage:image forState:UIControlStateNormal];
    [button setTintColor:[UIColor whiteColor]];
    [button setTitle:@"Done" forState:UIControlStateNormal];
    button.centerY = testLessonView.height * 0.5;
    
    [testLessonView addSubview:lessonTimeLabel];
    [testLessonView addSubview:lessonNameLabel];
    [testLessonView addSubview:button];
    [self.view addSubview:testLessonView];
}
-(void)doneButtonClick
{
    NSMutableArray *array = [NSMutableArray arrayWithArray: self.navigationController.childViewControllers];
    [array removeAllObjects];
    PTRecorderViewController *recoderVc = [[PTRecorderViewController alloc]init];
    self.recoderVc = recoderVc;
    [array addObject:recoderVc];
    self.navigationController.viewControllers = array;
    //            LoginViewController *Vc = [[LoginViewController alloc]init];
    //            [self.navigationController pushViewC ßontroller:Vc animated:YES];
    
    
//    [self.navigationController popToRootViewControllerAnimated:YES];

    
//    [self.navigationController pushViewController:recoderVc animated:YES];
}
-(void)setupTeacherMessageView
{
    PTTeacherMessageView *teacherMessageView = [[PTTeacherMessageView alloc]initWithFrame:CGRectMake(0, 64, 320, 200)];
    teacherMessageView.meItem = self.meItem;
    teacherMessageView.height = teacherMessageView.cellHeight;
    [self.view addSubview:teacherMessageView];
    [teacherMessageView.rightArrowButton addTarget:self action:@selector(rightArrowButtonClick) forControlEvents:UIControlEventTouchUpInside];

    
    NSString *string = @"I prefer to use voice messages, so for those don't have an iPhone like me, I need an app.";
    UILabel *mottoLabel = [[UILabel alloc]init];
    mottoLabel.textColor = [UIColor lightGrayColor];
    mottoLabel.font = [UIFont systemFontOfSize:13];

    mottoLabel.numberOfLines = 0;

    
    NSMutableAttributedString * attributedString1 = [[NSMutableAttributedString alloc] initWithString:string];
    NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle1 setLineSpacing:8];
    [attributedString1 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [string length])];
    [mottoLabel setAttributedText:attributedString1];
    mottoLabel.frame = (CGRect){{12 , 12 },{ 298 , 0}};
    [mottoLabel sizeToFit];

    UIView *mottoView = [[UIView alloc]initWithFrame:CGRectMake(0, teacherMessageView.cellHeight + 65, 320, mottoLabel.size.height + 24)];
    [mottoView addSubview:mottoLabel];
    mottoView.backgroundColor = [UIColor whiteColor];
    self.mottoView = mottoView;
    [self.view addSubview:mottoView];
}
-(void)rightArrowButtonClick
{
    TeacherViewController *teacherDetailView = [[TeacherViewController alloc]init];
    teacherDetailView.navigationItem.title = self.meItem.teacherName;
    [self.navigationController pushViewController:teacherDetailView animated:YES];
    
}
-(void)setupWeekTime
{
    
    UIView *weekTiemView = [[UIView alloc]initWithFrame:CGRectMake(12, CGRectGetMaxY(self.mottoView.frame), 308 , 100)];
    
    CGFloat width = weekTiemView.width / 7.0 ;
//    CGFloat height = weekTiemView.height;
    NSDateFormatter *outputFormat1 = [[NSDateFormatter alloc]init];
    [outputFormat1 setDateFormat:@"EEE"];
    NSDateFormatter *outputFormat2 = [[NSDateFormatter alloc]init];
    [outputFormat2 setDateFormat:@"d"];
    NSDate *today = [[NSDate alloc]init];
    NSTimeInterval secondsPerDay = (NSTimeInterval)24*60*60 ;
    
    NSString *string1 = @"Today";
    NSString *string2 = [outputFormat2 stringFromDate:today];
    for (int i = 0; i < 7; i++) {
        
        UILabel *weekLabel1 = [[UILabel alloc]initWithFrame:CGRectMake(i * width, 5 , width , 30)];
        weekLabel1.font = [UIFont systemFontOfSize:15];
        weekLabel1.textColor = PTColor(75, 77, 84, 1.0);
        weekLabel1.textAlignment = NSTextAlignmentCenter;
        UIImage *image = [UIImage imageNamed:@"daySelect"];
        UIButton *dayButton1 = [[UIButton alloc]initWithFrame:CGRectMake(i * width, 35 , image.size.width , image.size.height)];
        dayButton1.centerX = weekLabel1.centerX;
        dayButton1.tag = i;
        weekLabel1.text = string1;
        [dayButton1 setTitle:string2 forState:UIControlStateNormal];
        [dayButton1 setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        [dayButton1 setBackgroundImage:[UIImage imageNamed:@"daySelect"] forState:UIControlStateSelected];
        [dayButton1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [dayButton1 addTarget:self action:@selector(dayButtonClick:) forControlEvents:UIControlEventTouchUpInside
         ];
        [weekTiemView addSubview:weekLabel1];
        [weekTiemView addSubview:dayButton1];
        today = [today dateByAddingTimeInterval: secondsPerDay];
        string1 = [outputFormat1 stringFromDate:today];
        string2 = [outputFormat2 stringFromDate:today];

    }
    UIButton *selectButton = weekTiemView.subviews[1] ;
    selectButton.selected = YES;
    self.lastSelectedButton = selectButton;
    weekTiemView.frame = CGRectMake(10, CGRectGetMaxY(self.mottoView.frame), 300 , CGRectGetMaxY(selectButton.frame) + 15);
    [weekTiemView setUserInteractionEnabled:YES];
    [self.view addSubview:weekTiemView];
    self.weekTiemView = weekTiemView;
}

-(void)dayButtonClick:(UIButton *)button
{
    self.lastSelectedButton.selected = NO;
    button.selected = YES;
    self.lastSelectedButton = button;
    [self.timeSelectView reloadData];
}


-(void)setupTimeSelectView
{
    UITableView *timeSelectView = [[UITableView alloc]initWithFrame:CGRectMake(0 , CGRectGetMaxY(self.weekTiemView.frame) , 320 , self.view.height - CGRectGetMaxY(self.weekTiemView.frame ) - 56) style:UITableViewStylePlain];

    timeSelectView.backgroundColor = PTColor(245, 245, 245, 1.0);
    timeSelectView.delegate = self;
    timeSelectView.dataSource = self;
    [self.view addSubview:timeSelectView];
    self.timeSelectView = timeSelectView;
    
}
- (void)setMeItem:(MeItem *)meItem
{
    
    _meItem = meItem;
    

}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    PTStudyAppointmentTimeSelectCell *cell = (PTStudyAppointmentTimeSelectCell *)[tableView cellForRowAtIndexPath:indexPath];
    PTStudyAppointmentTimeSelectCellView *timeSelect =  cell.timeSelectView;
    
    PTStudyAppointmentTimeSelectCell *lastCell = (PTStudyAppointmentTimeSelectCell *)[tableView cellForRowAtIndexPath:self.lastTimeSelectindexPath];
    PTStudyAppointmentTimeSelectCellView *LastTimeSelect =  lastCell.timeSelectView;
    
    LastTimeSelect.arrowButton.selected = NO;
    timeSelect.arrowButton.selected = YES;
    self.lastTimeSelectindexPath = indexPath;
//    [timeSelect buttonClick:timeSelect.arrowButton];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 42;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PTStudyAppointmentTimeSelectCell *cell = [PTStudyAppointmentTimeSelectCell cellWithTableView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    
    //cell.meItem = self.meItem;
    
    return cell;
}
@end
